package com.cloudpolice.door.unit;

import com.cloudpolice.door.constant.QueueEnum;
import lombok.extern.log4j.Log4j2;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

/**
 * @author lianghao
 * @create 2019-12-15 10:06
 */
@Log4j2
@Component
public class RabbitMQUtil {
    @Autowired
    private AmqpTemplate rabbitTemplate;

    @Autowired
    @Qualifier("getStaticLibQueueEnum")
    private QueueEnum queueEnum;
    @Autowired
    @Qualifier("getVerifaceQueueEnum")
    private QueueEnum queueEnum2;

    /**
     * 发送到RabbitMQ
     *
     * @param data 发送数据
     * @return 是否发送成功
     */
    public <T> boolean send(T data) {
        boolean sendSign = false;
        try {
            this.rabbitTemplate.convertAndSend(queueEnum.getQueueName(), data.toString());
            sendSign = true;
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
        return sendSign;
    }

    /**
     * 发送到RabbitMQ
     *
     * @param data 发送数据
     * @return 是否发送成功
     */
    public <T> boolean send2(T data) {
        boolean sendSign = false;
        try {
            this.rabbitTemplate.convertAndSend(queueEnum2.getQueueName(), data);
            sendSign = true;
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
        return sendSign;
    }
}
